﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatentApp.Data;
using PatentApp.Models;

namespace PatentApp
{
    public partial class dlgUpdatePayment : Form
    {
        public IList<Produkt> produkts;
        public int Index { get { return index; } }
        int index = 0;

        public dlgUpdatePayment()
        {
            InitializeComponent();            
            using (var session = NHibernateCfg.OpenSession()){
                this.produkts = (from prods in session.QueryOver<Produkt>() where prods.Platnosc &&
                                    (prods.DataZgloszenia.Value >= DateTime.Now.AddYears(-20) && prods.DataZgloszenia.Value <= DateTime.Now.AddYears(-20).AddMonths(4))
                                 orderby prods.DataZgloszenia
                                 select prods).Asc.List<Produkt>();
            }
            lblDateNow.Text = "Dziś jest " + DateTime.Now.ToString("dddd \r\n") 
                + DateTime.Now.ToString(" dd MMMM yyyy \r\n")+
                "Godzina " + DateTime.Now.ToString("HH:mm:ss");
            ReLoadForm();
        }

        public void ReLoadForm()
        {
            if (produkts == null || produkts.Count == 0)
                return;
            int range = 0;
            txtDate.Text = produkts[index].DataZgloszenia.Value.ToShortDateString();
            txtID.Text = produkts[index].ProduktId.ToString();
            txtNumOgol.Text = produkts[index].NrOgolny.ToString();
            txtNumRocz.Text = produkts[index].NrRoczny.ToString();
            txtPatentNum.Text = produkts[index].NrPatentu.ToString();
            chkPlatnosc.Checked = produkts[index].Platnosc;
            if (!chkPlatnosc.Checked)
                btnEnd.Enabled = false;
            txtWlascicielPraw.Text = produkts[index].WlascicielPraw;
            if (produkts[index].DataZgloszenia.Value.AddYears(20) <= DateTime.Now)
                range = 5;
            else if (produkts[index].DataZgloszenia.Value.AddYears(20) <= DateTime.Now.AddMonths(1))
                range = 1;
            else if (produkts[index].DataZgloszenia.Value.AddYears(20) <= DateTime.Now.AddMonths(2))                
                range = 2;
            else if (produkts[index].DataZgloszenia.Value.AddYears(20) <= DateTime.Now.AddMonths(3))
                range = 3;
            else if (produkts[index].DataZgloszenia.Value.AddYears(20) <= DateTime.Now.AddMonths(4))
                range = 4;

            switch (range)
            {
                case 1:
                    txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.Red;
                    break;
                case 2:
                    txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.Orange;
                    break;                                                                                                     
                case 3:                                                                                                        
                    txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.Yellow;
                    break;                                                                                                     
                case 4:                                                                                                        
                    txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.Green;
                    break;                                                                                                     
                default:                                                                                                       
                    txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.White;
                    break;
            }           
        }

        public void ClearForm() {
            txtDate.Text = txtID.Text = txtNumOgol.Text = txtNumRocz.Text = txtPatentNum.Text = txtWlascicielPraw.Text = "";
            txtDate.BackColor = txtID.BackColor = txtNumOgol.BackColor = txtNumRocz.BackColor = txtPatentNum.BackColor = txtWlascicielPraw.BackColor = Color.White;
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (index > 0)
                index--;
            ReLoadForm();
        }

        private void btnNex_Click(object sender, EventArgs e)
        {
            if (index + 1 < produkts.Count)
                index++;
            else
                MessageBox.Show("Przeszukiwanie bazy zakończone. Niestwierdzono potrzeby aktualizacji.");
            ReLoadForm();
        }
        
        private void btnMainWindow_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            if(produkts.Count==0)
                return;
            using (var session = NHibernateCfg.OpenSession()){
                produkts[index].Platnosc = !produkts[index].Platnosc;
                Produkt pr = (from prod in session.QueryOver<Produkt>() where prod.ProduktId == produkts[index].ProduktId select prod).SingleOrDefault<Produkt>();
                pr.Platnosc = produkts[index].Platnosc;
                session.SaveOrUpdate(pr);
                session.Flush();
            }
            using (var session = NHibernateCfg.OpenSession())
            {
                Produkt pr2 = (from prod in session.QueryOver<Produkt>() where prod.ProduktId == produkts[index].ProduktId select prod).SingleOrDefault<Produkt>();
            }
            ReLoadForm();
        }

        private void btnDetails_Click(object sender, EventArgs e)
        {
            new dlgDocs(Application.StartupPath + "\\Documents\\Aktualizacja").ShowDialog();
        }

        private void chkPlatnosc_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
